package com.power.project.controller;

import com.power.base.model.ModelUtil;
import com.power.common.domain.PageDataResponse;
import com.power.common.domain.ResponseDTO;
import com.power.project.model.bo.ProjectSaveBO;
import com.power.project.model.bo.ProjectUserSaveBO;
import com.power.project.model.pageInfo.P_projectuserPageInfo;
import com.power.project.model.vo.ProjectVO;
import com.power.project.model.vo.UserBaseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.power.project.model.P_project;
import com.power.project.model.pageInfo.P_projectPageInfo;
import com.power.project.api.IP_projectFeign;
import com.power.project.service.P_projectService;

import javax.annotation.Resource;
import javax.validation.Valid;



/**
 *   项目控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "P_projectController",tags={"项目"})
public class P_projectController implements IP_projectFeign{

    @Resource
    private P_projectService p_projectService;

    
    @Override
    @ApiOperation("查询项目分页列表")
    public ResponseDTO<PageDataResponse<ProjectVO>> list(P_projectPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<ProjectVO> page = this.p_projectService.findAll(pageInfo);
        PageDataResponse<ProjectVO> p = new PageDataResponse<>(page.getContent(), page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看项目")
    public ResponseDTO<P_project> view(Integer id) {
        Assert.notNull(id, "id不能为空");
        P_project p_project  = p_projectService.getById(id);
        if(p_project != null){
            ModelUtil.geneText(p_project);
        }
        return ResponseDTO.succData(p_project);
    }

    @Override
    @ApiOperation("删除项目")
    public ResponseDTO<Void> delete(Integer id) {
        Assert.notNull(id, "id不能为空");
        p_projectService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改项目")
    public ResponseDTO<Void> save(@Valid @RequestBody ProjectSaveBO saveBO) {
        p_projectService.save(saveBO);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="查询项目关联人员")
    public ResponseDTO<PageDataResponse<UserBaseVO>> queryUsersByPid(P_projectuserPageInfo pageInfo) {
        Assert.notNull(pageInfo,"pageInfo参数不能为空");
        Assert.notNull(pageInfo.getF_projectid(),"项目id不能为空");
        Page<UserBaseVO> page = this.p_projectService.queryUsersByPid(pageInfo);
        PageDataResponse<UserBaseVO> p = new PageDataResponse<>(page.getContent(), page.getTotalElements(), page.getSize(), Math.max(pageInfo.getPageNumber(), 1));;
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation(value = "添加项目关联人员")
    public ResponseDTO<Void> addUser(@Valid @RequestBody ProjectUserSaveBO puSaveBo) {
        Assert.notNull(puSaveBo,"pageInfo参数不能为空");
        Assert.notNull(puSaveBo.getUserIds(),"用户id参数不能为空");
        Assert.isTrue(!puSaveBo.getUserIds().isEmpty(),"用户id参数不能为空");
        Assert.notNull(puSaveBo.getProjectId(),"项目id参数不能为空");
        Assert.notNull(puSaveBo.getTenantid(),"租户id参数不能为空");
        p_projectService.addProjectuser(puSaveBo);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value = "移除项目中的关联人员")
    public ResponseDTO<Void> removeUser(ProjectUserSaveBO puSaveBo) {
        Assert.notNull(puSaveBo,"pageInfo参数不能为空");
        Assert.notNull(puSaveBo.getUserIds(),"用户id参数不能为空");
        Assert.notNull(puSaveBo.getProjectId(),"项目id参数不能为空");
        Assert.notNull(puSaveBo.getTenantid(),"租户id参数不能为空");
        p_projectService.removeProjectuser(puSaveBo);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value = "修改项目中的关联人员")
    public ResponseDTO<Void> editUser(ProjectUserSaveBO puSaveBo) {
        Assert.notNull(puSaveBo,"pageInfo参数不能为空");
        Assert.notNull(puSaveBo.getTenantid(),"租户id参数不能为空");
        p_projectService.saveProjectuser(puSaveBo);
        return ResponseDTO.succ();
    }


}
